<?php

namespace App\Models\Traits;

use Illuminate\Support\Facades\Hash;

trait HasUserIdentity
{
	public function isNormal()
	{
		return $this->grade === 'normal';
	}
	public function isNotNormal()
	{
		return $this->grade !== 'normal';
	}

	public function isVip()
	{
		return $this->grade === 'vip';
	}

	public function isNotVip()
	{
		return $this->grade !== 'vip';
	}

	public function isValidVip(): bool
	{
		return $this->isVip();
	}

	public function isNotValidVip(): bool
	{
		return !$this->isValidVip();
	}

	public function isValidGoldIntern(): bool
	{
		return $this->hasValidGoldRecord() and !$this->is_locked;
	}

	public function isNotValidGoldIntern(): bool
	{
		return !$this->isValidGoldIntern();
	}

	public function isValidGoldMaster(): bool
	{
        $needChildren = cs('storm')->each_upgrade_needs_children;
		return $this->myGoldRecordsChildrenCount() >= (int)$needChildren
            and !$this->is_locked;
	}

	public function isNotValidGoldMaster(): bool
	{
		return !$this->isValidGoldMaster();
	}

	public function isPhoneBound()
	{
		return (bool)$this->phone;
	}

	public function isWeixinBound()
	{
		return (bool)$this->wx_openid;
	}

	public function isWeiminiBound()
	{
		return (bool)$this->wm_openid;
	}

	public function isAlipayBound()
	{
		return (bool)$this->alipay_user_id;
	}

	public function isBankBound()
	{
		return (bool)$this->cert_number;
	}

	public function isPayCodeSet()
	{
		return (bool)$this->pay_code;
	}

	public function isFreelanceAuth()
	{
		return  isset($this->freelance->id_no) ? (bool)$this->freelance->id_no : 0 ;
	}

	public function isPayCodeValid($code)
	{
		if (empty($code)) return false;

		if (! Hash::check($code, $this->pay_code)) {
			return false;
		}

		return true;
	}
}
